#include "key.h"

extern KEY5 key;

uint8_t key_is_press(KEY5 *key)
{
    if (key->center)
        return 1;
    else if (key->up)
        return 2;
    else if (key->down)
        return 3;
    else if (key->left)
        return 4;
    else if (key->right)
        return 5;
    else
        return 0;
}

void key_clear(KEY5 *key_)
{
    key_->center = 0,
    key_->down = 0;
    key_->left = 0;
    key_->right = 0;
    key_->up = 0;
}

void key_init(void)
{
    pinMode(KEY_UP, INPUT_PULLUP);
    pinMode(KEY_DOWN, INPUT_PULLUP);
    pinMode(KEY_LEFT, INPUT_PULLUP);
    pinMode(KEY_RIGHT, INPUT_PULLUP);
    pinMode(KEY_CENTER, INPUT_PULLUP);

    attachInterrupt(KEY_UP, isr_key_up, FALLING);         // 下降沿触发
    attachInterrupt(KEY_DOWN, isr_key_down, FALLING);     // 下降沿触发
    attachInterrupt(KEY_LEFT, isr_key_left, FALLING);     // 下降沿触发
    attachInterrupt(KEY_RIGHT, isr_key_right, FALLING);   // 下降沿触发
    attachInterrupt(KEY_CENTER, isr_key_center, FALLING); // 下降沿触发
}

void IRAM_ATTR isr_key_up()
{
    key.up = 1;
}

void IRAM_ATTR isr_key_down()
{
    key.down = 1;
}
void IRAM_ATTR isr_key_center()
{
    key.center = 1;
}
void IRAM_ATTR isr_key_left()
{
    key.left = 1;
}
void IRAM_ATTR isr_key_right()
{
    key.right = 1;
}
